(function(){
	angular.module('app').directive('ngCompare', ['$rootScope', function($rootScope) {

    function inherit(child, parent) {
        child.prototype = new parent();
        child.prototype.constructor = child;
    }

    function CompareUtil() {}

    CompareUtil.prototype.compareResult = function() {
        var compare = this.getCompare(this.compare);
        var compareTo = this.getCompareTo(this.compareTo);
        // console.log('compare:'+compare+" compareTo:"+compareTo);
        // console.log(this.op);
        switch (this.op) {
            case 'eqeq':
                // 等于
                return compare == compareTo;
                break;
            case 'eqeqeq':
                // 全等于
                return compare === compareTo;
                break;
            case 'lt':
                // 小于
                return compare < compareTo;
                break;
            case 'gt':
                // 大于
                return compare > compareTo;
                break;
            case 'lteq':
                // 小于等于
                return compare <= compareTo;
                break;
            case 'gteq':
                // 大于等于
                return compare >= compareTo;
                break;
            case 'RegExp':
                // 正则匹配

                // console.log(compareTo); 
                if (compareTo) {
                    return compareTo.test(compare);
                }

                break;
            default:
                // statements_def
                console.log('op value is error:' + this.op);
                break;
        }
    }
    CompareUtil.prototype.getCompare = function(value) {
        //number
        if (!isNaN(value)) {
            return value * 1;
        }
        //date
        if (/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/.test(value)) {
            return new Date(value).getTime();
        }
        //string
        return value;
    }
    CompareUtil.prototype.getCompareTo = function(value) {
        // console.log('getCompareTo:')
        // console.log(value);
        if (this.op == "RegExp") {
            switch (value) {
                case 'mobile':
                    // 手机号
                    return /^1\d{10}$/;
                    break;
                case 'telephone':
                    // 电话
                    return /^0\d{2,3}-?\d{7,8}$/;
                    break;
                case 'email':
                    // 邮箱
                    return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                    break;
                case 'number':
                    // 数字 0-9
                    return /^[0-9]*$/;
                    break;
                case 'carded':
                    // 身份证号
                    return /^\d{15}|\d{18}$/;
                    break;
                default:
                    // statements_def
                    console.log('not define RegExp:' + value);
                    break;
            }
        } else {
            return this.getCompare(value);
        }
    }



    /**
     * @op  数值比较方式 eqeq,lt,gt
     * @compare  值
     * @compareTo 要比较的值
     */
    function Compare(op, compare, compareTo) {
        this.op = op;
        this.compare = compare;
        this.compareTo = compareTo;
    }

    inherit(Compare, CompareUtil);


    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl) {

            var msg = attrs.msg;

            scope.Ctrl = ctrl;
            var op = attrs.compareOp;
            ctrl.$validators.compareTo = function(val) {
                // console.log(attrs.compareTo);
                if (ctrl.$isEmpty(val)) {
                    // consider empty models to be valid
                    return true;
                }
                if (op == "RegExp") {
                    var compareTo = attrs.compareTo;
                    // console.log('RegExp');
                } else {
                    var compareTo = scope.$eval(attrs.compareTo);
                    //compareTo = compareTo==''?0:compareTo;
                    // console.log(compareTo);

                }

                var compare = new Compare(op, val, compareTo);
                console.log('val:' + val + " " + op + " compareTo:" + compareTo + " is " + compare.compareResult())

                return compare.compareResult();
                // return val == scope.$eval(attrs.compareTo);
            };

            scope.$watch(attrs.compareTo, function() {
                ctrl.$validate();
            });
            scope.$on('$destroy', function() {
                console.log('destory compare');
                compare = null;
                delete compare;
            });
            /*scope.$watch(function(){
				console.log('******');
				console.log(ctrl.$invalid);
				if(ctrl.$invalid){
            		$(element).popup();
            	}else{
            		if($(element).popup('exists')){

            		}else{
            			$(element).popup({
            				on:'hover',
            				content: msg
            			});
            		}
            		
            	}
			});        
			console.log(scope.Ctrl);
            scope.$watch('Ctrl.$valid',function(validity){
            	console.log('Ctrl.$valid');
            	console.log(validity);
            	if(validity){
            		$(element).popup();
            	}else{
            		if($(element).popup('exists')){

            		}else{
            			$(element).popup({
            				on:'hover',
            				content: msg
            			});
            		}
            		
            	}
            },true);*/
        }
    };
}]);
})();